home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5023 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  105 lines

  1. Path: news.mcs.net!mdp
  2. From: mdp@mika-sys.com (Michael D. Perry)
  3. Newsgroups: comp.lang.c
  4. Subject: Bitwise Operators, Help with please!
  5. Date: Sat, 10 Feb 96 23:43:58 GMT
  6. Organization: MIKA Systems
  7. Message-ID: <4fjaju$i1o_001@pr.mcs.net>
  8. NNTP-Posting-Host: mdp.pr.mcs.net
  9. X-Newsreader: News Xpress Version 1.0 Beta #4
  10.  
  11. Hello All;
  12.  
  13. I am trying to teach myself 'C' programming from a book which 
  14. perhaps is not explaining the Bitwise Operators as well as I 
  15. would desire in order to understand them.
  16.  
  17. After going through a few examples the book is having me write a 
  18. program that will pack som data and then unpak using bitwise 
  19. operators.  I have the packing down OK and can print the bits but 
  20. am just not getting the Unpacking.
  21.  
  22. What I want to do is to unpack each 'field' of a type short with 
  23. the data as follows:
  24.  
  25. Identification        Job Type    Gender
  26. ==============        ========    ======
  27. bbbbbbbbb        bbbbbb        b
  28.  
  29. and then be able to read the returned bits and conver them back 
  30. to their original 'readable' form.
  31.  
  32. Can someone help me with some code so I can at least see how this 
  33. would be done and maybe understand it then?
  34.  
  35. What I have so far is listed below and thanks for your 
  36. assistance in advance.
  37.  
  38. Mike
  39.  
  40.  
  41. = = = = = = = = = = 
  42.  
  43. #include <stdio.h>
  44. #include <limits.h>
  45.  
  46. short    create_employee_data(int, int, char);
  47. void    bit_print(int);
  48. short    unpak(int, int);
  49.  
  50. main()
  51. {
  52.     int    id_no = 255;
  53.     int    job_type = 63;
  54.     char    gender    = 'F';
  55.     short   employee;
  56.  
  57.     employee = create_employee_data(id_no,
  58.                     job_type,
  59.                     gender);
  60.     bit_print(employee);
  61.     printf("\n%d", char_unpak(employee, 32766));
  62.     return 0;
  63. }
  64.  
  65. short    create_employee_data(int id_no,
  66.                 int job_type,
  67.                 char gender)
  68. {
  69.     short employee    =    0;
  70.  
  71.     employee |= (gender == 'm' || gender == 'M') ? 0 : 1;
  72.     employee |= (job_type << 1);
  73.     employee |= (id_no << 7);
  74.     return employee;
  75. }
  76.  
  77. void    bit_print(int a)
  78. {
  79.     int    i;
  80.     int   n    =    sizeof(int) * CHAR_BIT;
  81.     int    mask    =    1 << (n - 1);
  82.  
  83.     for (i = 1; i <= n; ++i)
  84.     {
  85.         putchar(((a & mask) == 0) ? '0' : '1');
  86.         a <<= 1;
  87.         if (i % CHAR_BIT == 0 && i < n)
  88.         {
  89.             putchar(' ');
  90.         }
  91.     }
  92. }
  93.  
  94. short    unpak(int emp, int move, unsigned mask)
  95. {
  96.     return ((emp & mask)>> move);
  97. }
  98.  
  99. -------------------------------------------------------------------
  100. Michael D. Perry      |   FIRST RULE OF INTELLIGENT TINKERING:
  101. MIKA Systems          |       Save all the parts.
  102. Glen Ellyn, Illinois    |   SATTINGER'S LAW:
  103. mdp@mika-sys.com |       It works better if you plug it in.
  104. -------------------------------------------------------------------
  105.